import { createReducer } from 'reduxsauce'
import { REHYDRATE } from 'redux-persist/es/constants'
import {
  GET_TEMPLATE_AND_PACKS,
  GET_TEMPLATE_OPTIONS_DETAIL,
  SET_SERVICE_PACK,
  GET_SERVICE_PACK_DETAIL,
  GET_ORDER_PACKS,
} from '@ACTIONS/servicePack'
import { SAVE_PATIENT } from '@ACTIONS/patient'

const INITIAL_STATE = {
  templateAndPacks: [],
  loadingTemplateAndPacks: true,
  canLoadMoreTemplateAndPacks: true,
  initLoadingTemplateAndPacks: true,
  templateDetail: null,
  loadingTemplateDetail: true,
  loadingServicePackDetail: true,
  servicePackDetail: {},
  orderPacks: [],
  loadingOrderPacks: true,
  canLoadMoreOrderPacks: true,
  initLoadingOrderPacks: true,
}

const rehydrate = (state, { payload }) => {
  if (payload && payload.servicePack) {
    return { ...INITIAL_STATE, ...payload.servicePack }
  }
  return { ...state }
}

export default createReducer(INITIAL_STATE, {
  [REHYDRATE]: rehydrate,

  [GET_TEMPLATE_AND_PACKS.REQUEST]: (state, { payload: { skip } = {} }) => ({
    ...state,
    loadingTemplateAndPacks: true,
    initLoadingTemplateAndPacks: skip === 0 ? true : state.initLoadingTemplateAndPacks,
    templateAndPacks: skip === 0 ? [] : state.templateAndPacks,
  }),
  [GET_TEMPLATE_AND_PACKS.SUCCESS]: (state, { data, canLoadMore }) => ({
    ...state,
    templateAndPacks: data,
    loadingTemplateAndPacks: false,
    canLoadMoreTemplateAndPacks: canLoadMore,
    initLoadingTemplateAndPacks: false,
  }),
  [GET_TEMPLATE_AND_PACKS.FAILURE]: state => ({ ...state, loadingTemplateAndPacks: false }),

  [GET_TEMPLATE_OPTIONS_DETAIL.REQUEST]: state => ({ ...state, templateDetail: null, loadingTemplateDetail: true }),
  [GET_TEMPLATE_OPTIONS_DETAIL.SUCCESS]: (state, { data, isAsync }) => {
    const { templateAndPacks: preTemplateAndPacks } = state
    const { p8Id, LatestPack } = data
    let templateAndPacks = preTemplateAndPacks
    if (isAsync) {
      templateAndPacks = preTemplateAndPacks?.map(item => {
        const { MerchandiseOptions } = item
        const isSameId = Array.isArray(MerchandiseOptions) && MerchandiseOptions.find(m => m.TemplateId === p8Id)
        return (isSameId ? { ...item, LatestPack } : item)
      })
    }
    return {
      ...state,
      templateDetail: data,
      templateAndPacks,
      loadingTemplateDetail: false
    }
  },
  [GET_TEMPLATE_OPTIONS_DETAIL.FAILURE]: state => ({ ...state, loadingTemplateDetail: false }),

  [SET_SERVICE_PACK.REQUEST]: (state, { data }) => {
    const { templateAndPacks: preTemplateAndPacks } = state
    const templateAndPacks = preTemplateAndPacks.map(item => (item.p8Id === data.Template?.p8Id ? {
      ...item,
      LatestPack: data
    } : item))
    return ({
      ...state,
      servicePackDetail: data,
      templateAndPacks
    })
  },
  [GET_SERVICE_PACK_DETAIL.REQUEST]: state => ({ ...state, servicePackDetail: {}, loadingServicePackDetail: true }),
  [GET_SERVICE_PACK_DETAIL.SUCCESS]: (state, { data }) => ({
    ...state,
    servicePackDetail: data,
    loadingServicePackDetail: false
  }),
  [SAVE_PATIENT.SUCCESS]: (state, { result }) => {
    const patientId = state.servicePackDetail?.Patients?.[0]?.PatientId
    if (result?.PatientId === patientId) {
      return {
        ...state,
        servicePackDetail: { ...state.servicePackDetail, Patients: [result] }
      }
    }
    return state
  },
  [GET_ORDER_PACKS.REQUEST]: (state, { payload: { skip } = {} }) => ({
    ...state,
    loadingOrderPacks: true,
    initLoadingOrderPacks: skip === 0 ? true : state.initLoadingOrderPacks,
    orderPacks: skip === 0 ? [] : state.orderPacks,
  }),
  [GET_ORDER_PACKS.SUCCESS]: (state, { data, canLoadMore }) => ({
    ...state,
    orderPacks: data,
    loadingOrderPacks: false,
    canLoadMoreOrderPacks: canLoadMore,
    initLoadingOrderPacks: false,
  }),
  [GET_ORDER_PACKS.FAILURE]: state => ({ ...state, loadingOrderPacks: false }),
})
